#include "stdafx.h"
#include "cfixedframerate.h"
#include <windows.h>

CFixedFrameRate::CFixedFrameRate(int frameRate)
{
	m_frameRate = frameRate;
	QueryPerformanceCounter((LARGE_INTEGER *)&m_c0);
}
CFixedFrameRate::~CFixedFrameRate()
{

}
double CFixedFrameRate::getFrameLength()
{
	return 1.0/(double)m_frameRate;
}
void CFixedFrameRate::endFrame()
{
	__int64 c1,c,cps;
	double freq;
	int res;

	QueryPerformanceCounter((LARGE_INTEGER *)&c1);
	c = c1-m_c0;

	QueryPerformanceFrequency((LARGE_INTEGER *)&cps);//seaactualiza el contador por segundo
	freq = 1.0/(double)cps;
	
	res = m_frameRate-(int)freq*c;
	Sleep(res*1);
	
	QueryPerformanceCounter((LARGE_INTEGER *)&m_c0);
}
